import { createRouter, createWebHashHistory } from 'vue-router'
import type { Router, RouteRecordRaw } from 'vue-router'
import pageRouter from './page'
import viewsRouter from './views'
import NProgress from 'nprogress'
import 'nprogress/nprogress.css'
import { getToken } from '@/utils/request'
import { clearLoginInfo } from '@/utils'

const router: Router = createRouter({
  history: createWebHashHistory(),
  routes: [...pageRouter, ...viewsRouter] as RouteRecordRaw[]
})

NProgress.configure({ showSpinner: false });

// 白名单路径
const whiteList: Array<string> = []

// router.beforeEach((to, _from, next) => {
//   NProgress.start()
//   document.title = (to.meta.title || '大屏设计器') as string
//   if(getToken()) {
//     if(to.path == '/login') {
//       clearLoginInfo()
//     }
//     next()
//   } else {
//     if(whiteList.includes(to.path)) {
//       next()
//     } else if(to.path == '/login') {
//       next()
//     } else {
//       next({ name: 'login' })
//     }
//   }
//   NProgress.done()
// })

router.beforeEach(to => {
  NProgress.start()
  document.title = (to.meta.title || '大屏设计器') as string
  if(getToken()) {
    if(to.path === '/login') {
      clearLoginInfo()
    }
  } else {
    if(!whiteList.includes(to.path) && to.path !== '/login') {
      return { name: 'login' }
    }
  }
  NProgress.done()
})

router.afterEach(() => {
  NProgress.done()
})

export default router